package com.ruoyi.project.channel.channelUserInfo.service;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.shiro.service.PasswordService;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.channel.businessInfo.domain.BusinessInfo;
import com.ruoyi.project.channel.smsInfo.service.ISmsInfoService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.channel.channelUserInfo.mapper.ChannelUserInfoMapper;
import com.ruoyi.project.channel.channelUserInfo.domain.ChannelUserInfo;
import com.ruoyi.project.channel.channelUserInfo.service.IChannelUserInfoService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 码商代理扩展 服务层实现
 * 
 * @author CAIN
 * @date 2019-10-29
 */
@Service
public class ChannelUserInfoServiceImpl implements IChannelUserInfoService 
{
	@Autowired
	private ChannelUserInfoMapper channelUserInfoMapper;
	@Autowired
	private IUserService userService;
	@Autowired
	private PasswordService passwordService;
	@Autowired
	private ISmsInfoService smsInfoService;
	/**
     * 查询码商代理扩展信息
     * 
     * @param channelUserId 码商代理扩展ID
     * @return 码商代理扩展信息
     */
    @Override
	public ChannelUserInfo selectChannelUserInfoById(Long channelUserId)
	{
	    return channelUserInfoMapper.selectChannelUserInfoById(channelUserId);
	}
	
	/**
     * 查询码商代理扩展列表
     * 
     * @param channelUserInfo 码商代理扩展信息
     * @return 码商代理扩展集合
     */
	@Override
	public List<ChannelUserInfo> selectChannelUserInfoList(ChannelUserInfo channelUserInfo)
	{
	    return channelUserInfoMapper.selectChannelUserInfoList(channelUserInfo);
	}
	
    /**
     * 新增码商代理扩展
     * 
     * @param channelUserInfo 码商代理扩展信息
     * @return 结果
     */
	@Override
	public int insertChannelUserInfo(ChannelUserInfo channelUserInfo, User user)
	{
		channelUserInfo.setChannelUserId(user.getUserId());
		channelUserInfo.setSalt(user.getSalt());
		channelUserInfo.setPayPwd(user.getPassword());
	    return channelUserInfoMapper.insertChannelUserInfo(channelUserInfo);
	}
	
	/**
     * 修改码商代理扩展
     * 
     * @param channelUserInfo 码商代理扩展信息
     * @return 结果
     */
	@Override
	public int updateChannelUserInfo(ChannelUserInfo channelUserInfo)
	{
	    return channelUserInfoMapper.updateChannelUserInfo(channelUserInfo);
	}

	/**
     * 删除码商代理扩展对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteChannelUserInfoByIds(String ids)
	{
		return channelUserInfoMapper.deleteChannelUserInfoByIds(Convert.toStrArray(ids));
	}
	/**
	 * 修改商家
	 *
	 * @param id          商家信息
	 * @param newPassword 新密码
	 * @return 结果
	 */
	@Override
	public AjaxResult resetSendPwd(String id, String newPassword, String code){
		// 当前操作者
		User user1 = ShiroUtils.getSysUser();
		boolean b = smsInfoService.validateCode(user1.getPhonenumber(),code.trim());
		if (!b) {
			return AjaxResult.error("手机验证码出错！");
		}
		ChannelUserInfo businessInfo = channelUserInfoMapper.selectChannelUserInfoById(Long.valueOf(id));
		User user = userService.selectUserById(businessInfo.getChannelUserId());
		businessInfo.setPayPwd(passwordService.encryptPassword(user.getLoginName(), newPassword, businessInfo.getSalt()));
		businessInfo.setUpdateBy(user1.getLoginName());
		businessInfo.setUpdateTime(new Date());
		channelUserInfoMapper.updateChannelUserInfo(businessInfo);
		return AjaxResult.success("重置成功！");
	}

	@Override
	public boolean matches(String password,Long id) {
		ChannelUserInfo businessInfo = selectChannelUserInfoById(id);
		if (businessInfo == null) {
			return false;
		}

		String newPwd = passwordService.encryptPassword(ShiroUtils.getLoginName(), password, businessInfo.getSalt());
		if (businessInfo.getPayPwd().equals(newPwd)) {
			return true;
		}
		return false;
	}
}
